package org.syg.generator.util;

import org.syg.generator.config.DataSourceConfig;
import org.syg.generator.entity.FreemarkerModel;
import org.syg.generator.entity.PropertyInfo;
import org.syg.generator.enums.DbType;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * @author ps
 */
public class EntityInfoUtil {

    public static List<FreemarkerModel> getPropertyInfo(String author, String packageDir, DataSourceConfig dbConfig, TreeSet<String> tableList) throws SQLException {
        if (tableList.isEmpty()) {
            throw new RuntimeException("请输入数据库表名");
        }
        String dbName = getDdName(dbConfig.getJdbcUrl());
        List<FreemarkerModel> list = new ArrayList<>();
        for (String tableName : tableList) {
            FreemarkerModel freemarkerModel = new FreemarkerModel();
            String sql = "select column_name,data_type,column_comment from information_schema.columns where table_schema='" + dbName + "' and table_name='" + tableName + "'";
            ResultSet rs = JdbcUtil.executeQuery(dbConfig, sql);
            List<PropertyInfo> columns = new ArrayList<>();
            while (rs.next()) {
                String column = rs.getString(1);
                String jdbcType = rs.getString(2);
                String comment = rs.getString(3);
                PropertyInfo property = new PropertyInfo();
                property.setColumn(column);
                property.setJdbcType(jdbcType);
                property.setComment(comment);
                property.setProperty(UnderToCamel.changeToJavaFiled(column));
                JdbcTypeToJavaType toJava = dbType(dbConfig);
                property.setJavaType(toJava.jdbcTypeToJavaType(jdbcType));
                columns.add(property);
            }
            if (columns.isEmpty()) {
                throw new RuntimeException("未能读取到表或表中的字段,请检查dbType,jdbcUrl,driverName,username,password是否正确");
            }
            freemarkerModel.setAuthor(author);
            freemarkerModel.setTableName(tableName);
            freemarkerModel.setFieldModels(columns);
            freemarkerModel.setPackageDir(packageDir);
            freemarkerModel.setClassName(UnderToCamel.getClassName(tableName));
            list.add(freemarkerModel);
        }
        return list;
    }

    public static JdbcTypeToJavaType dbType(DataSourceConfig dbConfig) {
        if (DbType.MYSQL.equals(dbConfig.getDbType())) {
            return new MySqlToJavaUtil();
        }
        return new OracleToJavaUtil();
    }

    /**
     * 获取url数据库名
     * @return
     */
    public static String getDdName(String jdbcUrl) {
        if ("".equals(jdbcUrl) || null == jdbcUrl) {
            throw new RuntimeException("jdbcUrl is empty");
        }
        if (!jdbcUrl.contains("?") || !jdbcUrl.contains("/")) {
            throw new RuntimeException("jdbcUrl is illegal");
        }

        return jdbcUrl.substring(jdbcUrl.lastIndexOf("/") + 1, jdbcUrl.indexOf("?"));
    }

}
